#include "stdafx.h"
#include "Spinner.h"
#include "../resource.h"
#include "GDI.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Spinner::Spinner(): m_active(false),m_focus(false),m_trigger(false)
{
	m_bitmap.LoadBitmap(MAKEINTRESOURCE(IDB_SPIN));

	BITMAP bm;
	m_bitmap.GetBitmap(&bm);
	m_bmsize.cx = bm.bmWidth;
	m_bmsize.cy = bm.bmHeight;

	m_cursor = ::LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_HAND));
}

Spinner::~Spinner()
{
	if(m_cursor){
		DestroyCursor(m_cursor);
	}
}

void Spinner::SetBounding(CRect bounding)
{
	CPoint O = bounding.CenterPoint();

	bounding.left = O.x-m_bmsize.cx/2;
	bounding.top = O.y-m_bmsize.cy/2;
	bounding.right = bounding.left + m_bmsize.cx;
	bounding.bottom = bounding.top + m_bmsize.cy;

	m_bounding = bounding;
}

void Spinner::DrawSpin(CDC *pDC)
{
	if( IsActive() )
	{
		DrawTransparentBmp(pDC->GetSafeHdc(), 
			(HBITMAP)m_bitmap, 
			m_bounding,
			RGB(0,0,0xFF)
			);
	}
}

void Spinner::Invalidate(HWND hwnd)
{
	::InvalidateRect(hwnd, &m_bounding, TRUE);
}

bool Spinner::OnMouseOver(CPoint point)
{
	CRect rect = m_bounding;
	rect.InflateRect(m_bmsize.cx*0.1, m_bmsize.cy*0.1);

	if( rect.PtInRect(point) ){
		SetFocus();
		return true;
	}

	SetFocus(false);
	return false;
}

bool Spinner::OnSetCursor()
{
	if( IsActive() )
	{
		if( IsFocus() ){
			::SetCursor(m_cursor);
			return true;
		}
	}

	return false;
}
